#include "CrumblingPlatformObject.h"

#include "..\Game.h"
#include "..\Definitions.h"
#include "GameObject.h"
#include "Player.h"
#include "Camera.h"
#include "..\Message Management\MessageID.h"
#include "..\Message Management\DeleteObjectMessage.h"

#include "..\..\SGD Wrappers\SGD_GraphicsManager.h"
#include "..\..\SGD Wrappers\SGD_Geometry.h"

CrumblingPlatformObject::CrumblingPlatformObject() : SGD::Message(MessageID::MSG_DELETE_OBJECT)
{
	SetID(9);
	bShaking = bNotDead = bPlayer = false;
	fFallTimer = 0.0f;
	fFallNow = .50f;
}


CrumblingPlatformObject::~CrumblingPlatformObject()
{
}

int CrumblingPlatformObject::Update(float dt)
{
	if (GetShaking())
	{
		// Increse fFallTimer by dt
		fFallTimer += dt;

		if (fFallTimer < fFallNow)
		{
			// Create Random values to simulate shaking
			int randomShakeX = rand() % 40 - 20;
			int randomShakeY = rand() % 24 - 12;
			SetPosition(SGD::Point{ GetPosition().x + randomShakeX * dt, GetPosition().y + randomShakeY * dt });
		}
		else if (fFallTimer >= fFallNow)
		{
			// Set Position changing y value
			SetPosition(SGD::Point{GetPosition().x, GetPosition().y + GetSize().height * 9.81f * dt});
		
			// If object falls off bottom of screen
			if (GetPosition().y >= GAME->GetScreenHeight() + Camera::GetInstance()->GetPosition().y)
			{
				// Send Delete Object Message
 				DeleteObjectMessage* pMsg = new DeleteObjectMessage(this);
				pMsg->QueueMessage();
				pMsg = nullptr;
			}
		}
	}

	return 0;
}

void CrumblingPlatformObject::Render()
{
	StationaryObject::Render();
}

void CrumblingPlatformObject::HandleCollision(const GameObject* collision)
{
	if(!GetShaking())
		SetShaking(true);

	// Get Player
	Player* player = GAME->GetPlayer();
	SGD::Point p = player->GetPosition();
	SGD::Rectangle pr = player->GetCollisionRect();
	SGD::Point e = GetPosition();
	int eh = (int)GetSize().height;

	if (collision->GetID() == PLAYER && collision->GetPosition().y - 30 > GetPosition().y - GetSize().height)
	{
 		player->SetVelocity(SGD::Vector{ 0.0f, player->GetVelocity().y });
		player->SetAcceleration(SGD::Vector{ 0.0f, player->GetAcceleration().y });
	}
	else //if (collision->GetID() == PLAYER && collision->GetPosition().y <= GetPosition().y - GetSize().height)
	{
		player->SetAcceleration(SGD::Vector{ player->GetAcceleration().x, 0.0f });
	 	player->SetVelocity(SGD::Vector{ player->GetVelocity().x, 0.0f });
		// Set player's y + height position to platform's top position
 		player->SetPosition(SGD::Point{ player->GetPosition().x, GetPosition().y - GetSize().height });

		// Set Animation to Idle if was jumping
		if (player->GetCurrentAnimationName() == "PlayerJumpLeft" || player->GetCurrentAnimationName() == "PlayerJumpRight")
		{
			GetAnimation()->IsPlaying(false);
			if (player->IsFacingLeft())
				player->SetAnimation("PlayerWalkLeft");
			else
				player->SetAnimation("PlayerWalkRight");
		}
		// Set player's IsOnCrumblingPlatformObject to allow jumping off
		player->IsOnCrumblingPlatformObject(true);
		player->IsOnWall(false);
		player->IsOnGround(false);
	}
}

bool CrumblingPlatformObject::GetShaking() const
{
	return bShaking;
}

bool CrumblingPlatformObject::GetFallNow() const
{
	return fFallTimer >= fFallNow;
}

void CrumblingPlatformObject::SetShaking(bool shaking)
{
	bShaking = shaking;
}